package org.afun.csms_android.adapter;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import org.afun.csms_android.R;
import org.afun.csms_android.domain.Course;
import org.afun.csms_android.domain.Stu2Score;
import org.afun.csms_android.domain.Student;
import org.afun.csms_android.domain.Teacher;
import org.afun.csms_android.domain.javaBean.SS;
import org.afun.csms_android.domain.javaBean.SS2;
import org.afun.csms_android.utils.MyTableOperator;
import org.afun.csms_android.utils.TableOperator;

import java.util.List;

public class Student2CourseSelectAdapter extends RecyclerView.Adapter<Student2CourseSelectAdapter.ViewHolder>{
    private List<SS2> mCourseList;
    private Context mContext;
    private Student student;

    static class ViewHolder extends RecyclerView.ViewHolder {
        View SelectCourseView;
        ImageView courseImage;
        TextView courseMsg;
        Button selectBtn;


        public ViewHolder(View view){
            super(view);
            SelectCourseView=view;
            courseImage=(ImageView) view.findViewById(R.id.selectcourse_image);
            courseMsg=view.findViewById(R.id.selectcourse_msg);
            selectBtn=view.findViewById(R.id.selectBtn);
        }

    }

    public Student2CourseSelectAdapter(List<SS2> courseList, Context context, Student student) {
        this.mCourseList=courseList;
        mContext=context;
        this.student=student;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.studentselectcourseitem, parent, false);
        final ViewHolder holder = new ViewHolder(view);

        holder.selectBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final int position = holder.getAdapterPosition();
                SS2 ss = mCourseList.get(position);
                // 选课时得指定选哪个教师
                LayoutInflater pointTeacherLayouxml=LayoutInflater.from(mContext);
                View myViewDialog = pointTeacherLayouxml.inflate(R.layout.student_select_teacher_dialog, null);
                RadioGroup radioGroup = myViewDialog.findViewById(R.id.selectTeacherRadioGroup);
                // 动态添加单选项
                List<Integer> teaId = ss.getTeaId();
                List<String> teaName = ss.getTeaName();
                for (int i = 0; i < teaId.size(); i++) {
                    RadioButton radioButton=new RadioButton(mContext);
                    RadioGroup.LayoutParams lp = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
                    radioButton.setId(teaId.get(i));
                    radioButton.setChecked(i == 0);
                    //设置文字
                    radioButton.setText(teaName.get(i));
                    radioButton.setTextSize(20);
//                    final int finalI = i;
//                    //设置radioButton的点击事件
//                    radioButton.setOnClickListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View v) {
//                            Toast.makeText(mContext, "this is radioButton  " + finalI + "    = " + v.getId(), Toast.LENGTH_SHORT).show();
//                        }
//                    });
                    //将radioButton添加到radioGroup中
                    radioGroup.addView(radioButton,lp);
                }

                AlertDialog alertDialog = new AlertDialog.Builder(mContext)
                        .setTitle("请选择教师")
                        .setView(myViewDialog)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                new AlertDialog.Builder(mContext).setTitle("提示")
                                        .setMessage("你是否确定选择为"+ss.getCouId()+"的课程？")
                                        .setPositiveButton("是", new DialogInterface.OnClickListener() {
                                            @SuppressLint("NotifyDataSetChanged")
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                RadioGroup radioGroup = myViewDialog.findViewById(R.id.selectTeacherRadioGroup);
                                                int teaId = radioGroup.getCheckedRadioButtonId();
                                                MyTableOperator myTableOperator=new MyTableOperator(mContext);
                                                TableOperator tableOperator = myTableOperator.getTableOperator();
                                                Stu2Score stu2Score = new Stu2Score();
                                                stu2Score.setStuId(student.getStuId());
                                                stu2Score.setCouId(ss.getCouId());
                                                stu2Score.setTeaId(teaId);
                                                boolean b = tableOperator.selectCourse(stu2Score);
                                                if(b){
                                                    new androidx.appcompat.app.AlertDialog.Builder(mContext)
                                                            .setTitle("提示信息")
                                                            .setMessage("选课成功！")
                                                            .setPositiveButton("确定",null)
                                                            .create()
                                                            .show();
                                                    mCourseList.remove(position);
                                                    notifyItemRemoved(position);
                                                    notifyItemRangeChanged(position,mCourseList.size());

                                                }else{
                                                    new androidx.appcompat.app.AlertDialog.Builder(mContext)
                                                            .setTitle("提示信息")
                                                            .setMessage("选课失败...")
                                                            .setPositiveButton("确定",null)
                                                            .create()
                                                            .show();
                                                }

                                            }
                                        })
                                        .setNegativeButton("否",null)
                                        .show();
                            }
                        })
                        .setNegativeButton("取消",null)
                        .create();
                    alertDialog.show();
            }
        });

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        SS2 ss = mCourseList.get(position);
        holder.courseImage.setImageResource(R.drawable.img);
        int couId = ss.getCouId();
        String couName = ss.getCouName();
//        String teaName = ss.getTeaName();

        holder.courseMsg.setText(
                "课程号："+couId+"\n课程名："+couName
        );




    }



    @Override
    public int getItemCount() {
        return mCourseList.size();
    }
}
